package com.hxzy.controller.admin.product;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.resp.PageResp;
import com.hxzy.common.vo.resp.R;
import com.hxzy.config.DishFlavorConfig;
import com.hxzy.controller.admin.login.resp.AdminLoginResp;
import com.hxzy.controller.admin.mapstruct.ProductConvert;
import com.hxzy.controller.admin.product.dto.DishFlavorProperty;
import com.hxzy.controller.admin.product.req.ProductAddReq;
import com.hxzy.controller.admin.product.req.ProductEditReq;
import com.hxzy.controller.admin.product.req.ProductReq;
import com.hxzy.controller.admin.product.resp.ProductResp;
import com.hxzy.entity.Product;
import com.hxzy.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/7/13 9:26
 * @Version 1.0
 */
@Api(tags = "管理后台-商品信息")
@RestController
@RequestMapping(value = "/api/product")
public class ProductController {


    @Autowired
    private ProductService productService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R<PageResp<ProductResp>> search(@Valid ProductReq req){

        AdminLoginResp adminLoginResp= WaimaiThreadLocal.getCurrent();
        //强制增强商家的ID
        if(adminLoginResp.getType()==2){
            req.setMerchantId(adminLoginResp.getId());
        }

        PageInfo<ProductResp>  pageInfo=this.productService.search(req);

        PageResp<ProductResp>  resp=new PageResp<>();
        resp.setTotal(pageInfo.getTotal());
        resp.setRows(pageInfo.getList());
        return R.okHasData(resp);
    }

    @ApiOperation(value = "口味列表数据")
    @GetMapping(value = "/dishflavor")
    public R<List<DishFlavorProperty>>  dishFlavorData(){
        Map entries = this.redisTemplate.opsForHash().entries(DishFlavorConfig.DISHFLAVOR_KEY);
        Collection<DishFlavorProperty> values = entries.values();
        List<DishFlavorProperty> arr=new ArrayList<>(values);
        return R.okHasData(arr);
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid ProductAddReq req){

       boolean result= this.productService.add(req);

        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R<ProductResp> findById(@PathVariable(value = "id") Long id){
        ProductResp resp = this.productService.findById(id);

        return R.okHasData(resp);
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public R update(@RequestBody @Valid ProductEditReq req){

        boolean result= this.productService.edit(req);

        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "商品上下架的处理")
    @GetMapping(value = "/isgrounding")
    public R upOrDown(Long[] ids,String status){
        if(StrUtil.isBlank(status)){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }
        if(ids==null || ids.length==0){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

        boolean result=this.productService.upOrDown(ids,status);

        return result?R.ok():R.build(AckCode.DATABASE_ERROR);
    }
}
